package org.lee.pattern.observer.demo;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.lee.pattern.observer.Observer;

/**
 * Thread2
 * 测试实例2
 *
 * @author dralee
 * @date 2022-03-09 10:22:30
 **/
@Slf4j
public class Thread2 implements Observer, Runnable {

    private static Thread CURRENT_THREAD;
    private static volatile boolean FLAG = false;

    @SneakyThrows
    @Override
    public void run() {
        CURRENT_THREAD = Thread.currentThread();
        log.info("thread2 running...");
        int count = 0;
        while (!FLAG) {
            log.info("value: {}", count++);
        }
        log.info("thread2 end.");
    }

    @Override
    public void exceptionHandler() {
        FLAG = true;
        CURRENT_THREAD.interrupt();
        log.info("thread2 rollback...");
    }
}
